<!DOCTYPE html>
<!--
This is a starter template page. Use this page to start your new project from
scratch. This page gets rid of all links and provides the needed markup only.
-->
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="x-ua-compatible" content="ie=edge">

  <title><%=SETTING.website_name%></title>

  <%- include(VIEWROOT+'common/cssjs.html'); %>
</head>

<body class="hold-transition sidebar-mini">
  <div class="wrapper">
    <h3 style="text-align: center;">文件类型：jpg,png,gif,rar,zip,pdf,txt,doc,docx,ppt,pptx,xls,xlsx,swf,iso最大2GB</h3>
          
    <div style="padding:20px;text-align: center;">
      <input type="file" name="" id="file">
      <button id="upload">上传</button>
      <span id="output" style="font-size:12px">等待</span>
    </div>


  </div>

  

<script>

  $('#upload').click(function(){
    if(!$("#file")[0].files[0]){
      alert('请先选择文件');
      return;	
    }
    var file = $("#file")[0].files[0],  //文件对象
      name = file.name,        //文件名
      size = file.size,        //总大小
      succeed = 0;
    var shardSize = 2 * 1024 * 1024,    //以2MB为一个分片
      shardCount = Math.ceil(size / shardSize);  //总片数
      
      
    window.start=function(i){
      //计算每一片的起始与结束位置
      var start = i * shardSize,
        end = Math.min(size, start + shardSize);
      //构造一个表单，FormData是HTML5新增的
      var form = new FormData();
      form.append("data", file.slice(start,end));  //slice方法用于切出文件的一部分
      form.append("name", name);
      form.append("total", shardCount);  //总片数
      form.append("index", i + 1);        //当前是第几片
      form.append("fileSize", file.size); //文件大小
      form.append("class", '<%=cls%>');
      form.append("i", '<%=i%>');
      form.append("callback", '<%=callback%>');
      form.append('uniq', '<%=uniq%>');
      //Ajax提交
      $.ajax({
        url: "<%=UPLOAD_HUGE%>",
        type: "POST",
        data: form,
        async: true,        //异步
        processData: false,  //很重要，告诉jquery不要对form进行处理
        contentType: false,  //很重要，指定为false才能形成正确的Content-Type
        dataType: 'json',
        success: function(data){
          if(parseInt(data.result.code)!=0){
            alert(data.result.msg);
            return;	
          }else{
            if(data.result.url){
              alert('上传成功');
              if(typeof(window.parent.<%=callback%>)!='undefined'){
                window.parent.<%=callback%>(data.result);
              }else{
                console.log(data);
              }
              return;
            }
            
          }
          ++succeed;
          $("#output").text(succeed + " / " + shardCount + " [" + Math.round(succeed/shardCount*100) + "%]");
          ++i;
          if(i < shardCount) window.start(i);
        },
        error: function(){
          alert('上传失败');	
        }
      });
    }
    
    start(0);	
  });
  
</script>  
</body>

</html>